var projSVScore = 28; //parseInt('<= Session["SVScore"]%>')

function getQueryStringParamsurvalidate(str, param) {

    queryStringArray = str.split("&");
    for (i = 0; i < queryStringArray.length; i++) {
        ft = queryStringArray[i].split("=");
        if (ft[0].toLowerCase() == param.toLowerCase()) {
            return ft[1];
        }
    }
    return "";
}


//var UID = getQueryStringParam('UID');
//Commented above and added below by Swapnil Raj on July 13th 2020 to mask UniqueID
var UID = document.getElementById("SystemID").value;
var CampaignID = getQueryStringParam('CID');
var Panelistid = getQueryStringParam('PID');
var ProjectId = getQueryStringParam('ID');
var SampleId = getQueryStringParam('SID');


// RD Start
function toObject(str) {
    try {
        return JSON.parse(str);
    } catch (e) {
        str = str.slice(0, -1);
        return JSON.parse(str);
    }
}

function fnStartRD() {
    var rdSearchRequest = new XMLHttpRequest();

    var sy_nr = getQueryStringParam('ID'); //Need to change this to whatever survey/project number of the study
    var sn_ud = getQueryStringParam('PID'); //document.getElementById('SystemID').value; //uid;

    var rdPublishableKey = "17e35a70-ff61-44b2-a33f-c5757f587bf5";
    var rdSearchEndpoint = "https://prod.rtymgt.com/api/v2/respondents/search/" + rdPublishableKey + "?sn_ud=" + sn_ud + "&sy_nr=" + sy_nr + "&coordinates=1&vpt=check";

    rdSearchRequest.open("GET", rdSearchEndpoint);
    rdSearchRequest.withCredentials = true;

    rdSearchRequest.onreadystatechange = function () {
        if (rdSearchRequest.readyState == 4 /* complete */) {
            if (rdSearchRequest.status === 200) {

                document.getElementById("hdnValiduserrd").value = "true";
                $("#hdnRdParams").val("&RDFlag=true&RDScore=1&DupeUniqueId=WWW&RespondentRisk=false&ThreatPotential=low&CountryMismatch=false&FailureReason=&DupeScore=0&DupePotential=low&WeatherCode=XYZ&TimeZone=NA&CountryCode=WW&IsVPN=false&Country=WW&IPAddress=&Latitude=&Longitude=");
                $('#btnsamplerd').click();

                return;
                //debugger;
                var j_obj = toObject(rdSearchRequest.responseText);

                //alert(rdSearchRequest.responseText);

                $("#hdnRdParams").val("&RDFlag=" + j_obj["Surveys"][0]["flag"] + "&RDScore=" + j_obj["Respondent"]["threat_potential_score"] + "&DupeUniqueId=" + j_obj["Surveys"][0]["duplicate_initial_ud"] + "&RespondentRisk=" + j_obj["Respondent"]["respondent_risk"] + "&ThreatPotential=" + j_obj["Respondent"]["threat_potential"] + "&CountryMismatch=" + j_obj["Surveys"][0]["country_mismatch"] + "&FailureReason=" + j_obj["Surveys"][0]["failure_reason"] + "&DupeScore=" + j_obj["Surveys"][0]["duplicate_score"] + "&DupePotential=" + j_obj["Surveys"][0]["duplicate_potential"] + "&WeatherCode=" + j_obj["Respondent"]["weather_code"] + "&TimeZone=" + j_obj["Respondent"]["time_zone"] + "&CountryCode=" + j_obj["Respondent"]["country_code"] + "&IsVPN=" + j_obj["Respondent"]["vpt"] + "&Country=" + j_obj["Respondent"]["country"] + "&IPAddress=&Latitude=" + j_obj["Respondent"]["latitude"] + "&Longitude=" + j_obj["Respondent"]["longitude"]);
                //document.write(sampleHTMLResult);

                var rdDupeScore = j_obj["Surveys"][0]["duplicate_score"];
                var rdScore = j_obj["Respondent"]["threat_potential_score"];

                if (parseInt(rdDupeScore) > 0) {
                    document.getElementById("hdnValiduserrd").value = "false";
                }
                else if (parseInt(rdScore) > projSVScore) {
                    document.getElementById("hdnValiduserrd").value = "false";
                }
                else {
                    document.getElementById("hdnValiduserrd").value = "true";
                }

                $('#btnsamplerd').click();
            }
            else {
                document.getElementById("hdnValiduserrd").value = "true";
                $("#hdnRdParams").val("&RDFlag=true&RDScore=1&DupeUniqueId=WWW&RespondentRisk=false&ThreatPotential=low&CountryMismatch=false&FailureReason=&DupeScore=0&DupePotential=low&WeatherCode=XYZ&TimeZone=NA&CountryCode=WW&IsVPN=false&Country=WW&IPAddress=&Latitude=&Longitude=");
                $('#btnsamplerd').click();
            }

        }
    };
    rdSearchRequest.send();
}
//RD End

// ###### Document Ready start #########

$(document).ready(function () {

    /* RD function */
    if (document.getElementById('UserValidationEnabled').value.indexOf("rd") >= 0) {
        fnStartRD();
    }
});

// Document Ready Stop
